package com.ywjg.serviceimp;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.ywjg.dao.NewsMessageDao;
import com.ywjg.model.NewsMessage;
import com.ywjg.service.INewsMessageService;
import com.ywjg.utils.ApplicationModel;

@Service
public class NewsMessageService implements INewsMessageService {
	@Resource
	NewsMessageDao newsMessageDao;
	@Autowired
	ApplicationModel applicationModel;
	
	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.INewsMessageService#findNews(int, int)
	 */
	public List<NewsMessage> findNews(int page,int size){
		Pageable pageable = new PageRequest(page, size, new Sort(Direction.DESC, "timestamp"));
		Page<NewsMessage> nm = newsMessageDao.findAll(pageable);
		applicationModel.getMap().put("findNews", (int)nm.getNumberOfElements());
		List<NewsMessage> content = nm.getContent();
		return content;
	}
	
	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.INewsMessageService#addNews(com.ywjg.model.NewsMessage)
	 */
	public NewsMessage addNews(NewsMessage newsMessage){
		NewsMessage nm = newsMessageDao.save(newsMessage);
		return nm;
	}
	

}
